package cn.com.controller.manageController;


import cn.com.Enum.UserAuth;
import cn.com.annotation.Auth;
import cn.com.constant.Code;
import cn.com.db.model.Deck;
import cn.com.db.service.DeckService;
import cn.com.util.DataResult;
import cn.com.util.JsonUtil;
import cn.com.util.TimeUtil;
import cn.com.util.UidUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Auth(UserAuth.ADMIN)
@RequestMapping("/back/deck")
@RestController
public class DeckManageController {

    @Autowired
    DeckService deckService;

    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public String createDeck(Deck deck){
        DataResult dataResult=new DataResult();
        if (StringUtils.isNotBlank(deck.getParent())&&
                !deckService.isExist(deck.getParent())){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("父级不存在");
            return JsonUtil.toJsonString(dataResult);
        }
        deck.setId(UidUtil.createUUID());
        deck.setCreated_at(TimeUtil.getNowTimestamp());
        deck.setUpdated_at(TimeUtil.getNowTimestamp());
        deckService.createDeck(deck);
        dataResult.setData(deck);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("添加成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(method = RequestMethod.POST)
    public String getDeck(@RequestParam("parent")String parent){
        DataResult dataResult=new DataResult();
        dataResult.setTotal(deckService.countDeck(parent));
        dataResult.setData(deckService.getDeck(parent));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("返回成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "{id}",method = RequestMethod.GET)
    public String viewDeck(@PathVariable("id")String id){
        DataResult dataResult=new DataResult();
        if (!deckService.isExist(id)){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("装饰不存在");
            return JsonUtil.toJsonString(dataResult);
        }
        dataResult.setData(deckService.getDeckById(id));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("获取成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(Deck deck){
        DataResult dataResult=new DataResult();
        if (!deckService.isExist(deck.getId())){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("装饰不存在");
            return JsonUtil.toJsonString(dataResult);
        }
        if (StringUtils.isNotBlank(deck.getParent())&&
                !deckService.isExist(deck.getParent())){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("父级不存在");
            return JsonUtil.toJsonString(dataResult);
        }
        deckService.updateDeck(deck);
        dataResult.setData(deckService.getDeckById(deck.getId()));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("编辑成功");
        return JsonUtil.toJsonString(dataResult);
    }



}
